package com.frxuexi.shop.view

import android.content.Context
import android.os.Bundle
import android.widget.ImageView
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.adapter.ReplyAdapter
import com.frxuexi.lib_base.entity.ReplyBean
import com.frxuexi.lib_base.entity.ShopBeanItem
import com.frxuexi.lib_base.entity.ShopDetailBean
import com.frxuexi.lib_base.http.GlideUtil
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.room.MyShopBean
import com.frxuexi.shop.R
import com.frxuexi.shop.databinding.ActivityShopDetailBinding
import com.frxuexi.shop.intent.ShopIntent
import com.frxuexi.shop.viewmodel.ShopViewModel
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.youth.banner.loader.ImageLoader
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/shop/ShopDetailActivity")
@AndroidEntryPoint
class ShopDetailActivity : BaseActivity<ActivityShopDetailBinding,ShopIntent,ShopViewModel>() {
    override val binding: ActivityShopDetailBinding by lazy { ActivityShopDetailBinding.inflate(layoutInflater) }
    override val vm: ShopViewModel by viewModels<ShopViewModel>()
    @Autowired
    lateinit var sid:String
    val imgsList= mutableListOf<String>()
    lateinit var shopBean:ShopDetailBean

    val replyAdapter by lazy { ReplyAdapter() }

    var flag=false
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.DEFAULT->{
                                shopBean = it.data as ShopDetailBean
                                binding.shopTitle.setText(shopBean.productInfo.storeName)
                                binding.shopPrice.setText("￥${shopBean.productInfo.price}")

                            }
                            RequestType.ADD->{
                                ToastUtils.showLong("添加成功")
                                finish()
                            }
                            RequestType.LOVE->{
                                ToastUtils.showLong("收藏成功")
                                binding.shopLove.setImageResource(com.frxuexi.lib_base.R.drawable.ic_action_love)
                            }
                            RequestType.ISLOVE->{
                                val num = it.data as Int
                                if(num>0){
                                    binding.shopLove.setImageResource(com.frxuexi.lib_base.R.drawable.ic_action_love)
                                    flag=true
                                }else{
                                    binding.shopLove.setImageResource(com.frxuexi.lib_base.R.drawable.ic_action_nolove)
                                    flag=false
                                }
                            }
                            RequestType.DELETE->{
                                val num = it.data as Int
                                if(num>0){
                                    binding.shopLove.setImageResource(com.frxuexi.lib_base.R.drawable.ic_action_nolove)
                                }
                            }
                            RequestType.REPLY->{
                                replyAdapter.data.clear()
                                val replyBean = it.data as ReplyBean
                                replyAdapter.addData(replyBean.list)
                                replyAdapter.notifyDataSetChanged()
                            }

                            else -> {}
                        }


                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        vm.sendIntent(ShopIntent.getShopById(sid.toInt(),"normal"))
        vm.sendIntent(ShopIntent.isLove(sid.toInt()))

        imgsList.add("http://10.161.9.80:8080/crmebimage/public/maintain/2021/12/25/7a6475bba07c47bb98e5db0e0d21c8ebq5sw2kijlc.jpg")
        imgsList.add("http://10.161.9.80:8080/crmebimage/public/maintain/2021/12/25/37382cca048b4011bdbacd3cbbe0814daw1o5x0tco.jpg")
        imgsList.add("http://10.161.9.80:8080/crmebimage/public/maintain/2021/12/25/fef1aee22a04466d93693f5791d44bfd2rsm469mcr.jpg")
        binding.shopBanner.setImages(imgsList)
        binding.shopBanner.isAutoPlay(true)
        binding.shopBanner.setImageLoader(object :ImageLoader(){
            override fun displayImage(context: Context?, path: Any?, imageView: ImageView?) {
                GlideUtil.getImg(path!!.toString(),imageView!!)
            }

        })
        binding.shopBanner.start()

        binding.btnAddcart.setOnClickListener {
            val map = mapOf<String, Any>(
                "productId" to sid.toInt(),
                "cartNum" to 1,
                "productAttrUnique" to shopBean.productValue.默认.id.toString()
            )
            vm.sendIntent(ShopIntent.addCart(map))
        }

        binding.shopLove.setOnClickListener {
            if(flag){//取消收藏
                vm.sendIntent(ShopIntent.deleteLove(MyShopBean(shopBean.productInfo.id,shopBean.productInfo.storeName,shopBean.productInfo.price,shopBean.productInfo.image)))
            }else{//收藏
                vm.sendIntent(ShopIntent.addLove(MyShopBean(shopBean.productInfo.id,shopBean.productInfo.storeName,shopBean.productInfo.price,shopBean.productInfo.image)))
            }
            flag=!flag
        }

        binding.replyRv.apply {
            adapter=replyAdapter
            layoutManager=LinearLayoutManager(this@ShopDetailActivity)
        }

        binding.tabReply.apply {
            addTab(newTab().setText("全部"),true)
            addTab(newTab().setText("好评"),false)
            addTab(newTab().setText("中评"),false)
            addTab(newTab().setText("差评"),false)
            addOnTabSelectedListener(object :OnTabSelectedListener{
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    vm.sendIntent(ShopIntent.getReplys(sid.toInt(),10,1,tab?.position.toString()))
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {
                }

            })
        }
        vm.sendIntent(ShopIntent.getReplys(sid.toInt(),10,1,"0"))
    }
}